﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;

namespace ART.Common
{
    /// <summary>
    /// 加密操作类
    /// Create by Jinlong Zhang
    /// </summary>
    public class JSecury
    {

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始文字</param> 
        /// <param name="key">密钥</param> 
        /// <param name="encoding">字符编码方案</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(original, "MD5");
        }

        /// <summary>
        ///  解密
        /// </summary>
        /// 
        public static string DesDecrypt(string strText)
        {
            return DesDecrypt(strText, "#4$#^.web");
        }
        public static string DesDecrypt(string strText, string key)
        {
            if (strText=="")
            {
                return "";
            }
            string text = Utils.MD5(key);
            byte[] rgbKey = new byte[0];
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] buffer = new byte[strText.Length];
            try
            {
                rgbKey = Encoding.UTF8.GetBytes(text.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                buffer = Convert.FromBase64String(strText);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Encoding.UTF8.GetString(stream.ToArray());
            }
            catch //(Exception exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        /// 
        public static string DesEncrypt(string strText)
        {
            return DesEncrypt(strText, "#4$#^.web");
        }
        public static string DesEncrypt(string strText, string key)
        {
            if (strText == "")
            {
                return "";
            }
            string text = Utils.MD5(key);
            byte[] rgbKey = new byte[0];
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            try
            {
                rgbKey = Encoding.UTF8.GetBytes(text.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                byte[] bytes = Encoding.UTF8.GetBytes(strText);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                return Convert.ToBase64String(stream.ToArray());
            }
            catch //(Exception exception)
            {
                return "";
            }
        }
    }
}
